package org.liujing.jsw.client.ui.plaf;

import org.liujing.jsw.client.ui.*;
import com.google.gwt.core.client.GWT;
//import com.google.gwt.dom.client.*;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.*;

public class LabelUI extends ComponentUI{
	
	Dimension preferredSize;
	
	public LabelUI(){
		preferredSize = null;
	}
	
	
	public void installUI(JComponent c) {
		Element el = DOM.createDiv();
		c.setDomElement(el);
		setNoWrap(el);
		el.getStyle().setBackgroundColor("#ff9999");
    }
    
    private static native void setNoWrap(Element el)/*-{
    	el.style.whiteSpace = "nowrap";
    }-*/;
	
	public static class Factory implements ComponentUIFactory{
		//LabelUI ui = new LabelUI();
		
		public ComponentUI createUI(){
			GWT.log("createUI LabelUI");
			return new LabelUI();
		}
	}
	
	public void onComponentLoad(JComponent c){
		
		//Element e = c.getContentElement();
    	//Dimension dim = new Dimension(e.getClientWidth(), e.getClientHeight());
    	//GWT.log("labelUI.onComponentAttach() preferredSize: " + dim);
    	//preferredSize = dim;
    }
	
	@Override
	public Dimension getPreferredSize(JComponent c) {
		//GWT.log("LabelUI.getPreferredSize() - ");
		LabelProtype lb = (LabelProtype)c;
		Insets in = c.getInsets();
		Dimension dim = null;
		if(preferredSize == null){
			com.google.gwt.dom.client.Element e = c.getContentElement();
			preferredSize = new Dimension(e.getClientWidth(), e.getClientHeight());
			GWT.log("LabelUI.getPreferredSize() "+ preferredSize);
		}
		dim = new Dimension(preferredSize);		
		dim.width += (in.left + in.right);
		dim.height += (in.top + in.bottom);
		
		return dim;
    }
}
